<?php
namespace Seller\Controller;
use Think\Controller;
class ReturnGoodController extends CommonController {

    public function _initialize() {
        $params = array();
        parent::_initialize($params);
        $this->assign("now_module" ,"order");
    }

    //退换货列表
    public function index() {

        //初始化状态
        $this->assign('x_state', -2);

        //搜索功能
        $btn = I('get.btn');
        if ($btn == 1) {
            $order_id = I('get.order_id');
            $state = I('get.state');
            //订单编号的搜索
            if (!empty($order_id)) {
                $order = M('order')->where( array('order_id'=>$order_id) )->find();
                $map['a.order_id'] = $order['id'];
                $this->assign('x_order_id', $order_id);
            }

            //处理状态搜索
            if ($state != -2) {
                $map['a.state'] = $state;
                $this->assign('x_state', $state);
            }
        }



        $count = M('ReturnGood')->alias('a')
            ->join('LEFT JOIN __ORDER__ as o ON a.order_id=o.id')->where($map)->count();
        $page = set_page($count, C('PAGE_NUM'));
        $show = $page->show();
        $returnGood = M('ReturnGood')->alias('a')->field('o.order_id, a.now_time, a.id, a.state,ag.agent_name')
            ->join('LEFT JOIN __ORDER__ as o ON a.order_id=o.id')->where($map)
            ->join('LEFT JOIN __AGENT__ as ag ON a.agent_id=ag.agent_id')
            ->limit($page->firstRow.','.$page->listRows)->select();



        $state = array('-2'=>'处理状态','-1'=>'未通过','0'=>'未审核','1'=>'已通过');
        $this->assign('state', $state);
        $this->assign('returnGood', $returnGood);
        $this->assign('page',$show);
        $this->display();
    }



    //查看退换货
    public function detail() {
        $id = I('get.id');
        //退货申请详细信息
        $returnGood = D('ReturnGood')->relation(true)->where( array('id'=>$id) )->find();
        //订单里面的商品
        $orderGood = D('OrderGood')->relation(true)->where( array('order_id'=>$returnGood['order_id']) )->select();
        $this->assign('orderGood',$orderGood);
        $this->assign('returnGood',$returnGood);
        $this->assign('id', $id);
        $this->display();
    }


    //管理员退换货处理
    public function make() {
        $id = I('post.id');
        $data = array(
            'state' => I('post.state'),
            'info' => I('post.info'),
            'return_price' => I('post.return_price')
        );
        $bool = M('returnGood')->where( array('id'=>$id) )->save($data);
        $this->ajaxReturn($bool);
    }

    //退货原因列表
    public function reason() {
        $returnReason = M('returnReason')->order('id DESC')->select();
        $this->assign('returnReason',$returnReason);
        $this->display();
    }

    //添加退货原因
    public function addReason() {
        if (IS_AJAX) {
            $reason = I('post.reason');
            $data = array(
                'reason' => $reason,
                'now_time' => time()
            );
            $bool = M('returnReason')->add($data);
            $this->ajaxReturn($bool);
        } else {
            $this->display();
        }
    }

    //编辑退货原因
    public function editReason() {
        if (IS_AJAX) {
            $id = I('post.id');
            $data = array(
                'reason' => I('post.reason')
            );
            $bool = M('returnReason')->where( array('id'=>$id) )->save($data);
            $this->ajaxReturn($bool);
        } else {
            $id = I('get.id');
            $returnReason = M('returnReason')->where( array('id'=>$id) )->find();
            $this->assign('returnReason',$returnReason);
            $this->display();
        }
    }


    //删除退货原因
    public function delReason() {
        $map['id'] = I('post.id');
        $bool = M('returnReason')->where($map)->delete();
        $this->ajaxReturn($bool);
    }


















}